package cn.lyh.iotmanage.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lyh.iotmanage.dao.EmqTopicMsgDao;
import cn.lyh.iotmanage.dao.EmqTopicMsgDaoMapper;
/**
 * @author lyh
 * @version 1.0
 * @since 2021/5/20
 */
@Service
public class EmqTopicMsgDaoService extends ServiceImpl<EmqTopicMsgDaoMapper, EmqTopicMsgDao> {

    /**
     * 此接口用于存储从EMQ转发过来所有的topic上报消息
     * @param params 转发过来的JSON数据体
     */
    public void saveMassge(Map<String, Object> params) {
        // 1.提取时间戳等参数
        Object publishReceivedAt = params.get("publish_received_at");
        String username = (String)params.get("username");
        String topic = (String) params.get("topic");
        String payload = (String) params.get("payload");
        String clientid = (String) params.get("clientid");
        //2.格式化时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatTime = simpleDateFormat.format(publishReceivedAt);
        DateTime dateTime = DateUtil.parse(formatTime);
        //3.封装
        EmqTopicMsgDao emqTopicMsgDao = new EmqTopicMsgDao();
        emqTopicMsgDao.setUsername(username);
        emqTopicMsgDao.setTopicName(topic);
        emqTopicMsgDao.setPayload(payload);
        emqTopicMsgDao.setClientid(clientid);
        emqTopicMsgDao.setPublishReceivedAt(dateTime);
        System.out.println("emqTopicMsgDao = " + emqTopicMsgDao);
        //4.调用mapper存入mysql
        this.save(emqTopicMsgDao);


    }
}
